package com.fc.test.controller.admin;

import com.fc.test.common.base.BaseController;
import com.fc.test.common.domain.AjaxResult;
import com.fc.test.mapper.auto.TAbbGoodsMapper;
import com.fc.test.model.auto.TAbbGoods;
import com.fc.test.model.auto.TAbbGoodsExample;
import com.fc.test.model.custom.TableSplitResult;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.model.custom.TitleVo;
import com.fc.test.service.TAbbGoodsService;
import com.fc.test.util.DateUtil;
import com.fc.test.util.SnowflakeIdWorker;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//促销商品
@Controller
@RequestMapping("TAbbCGoodsController")
@Api(value = "")
public class TAbbCGoodsController extends BaseController{

	private String prefix = "admin/tAbbCGoods";
	@Autowired
	private TAbbGoodsService tAbbGoodsService;
	@Autowired
	private TAbbGoodsMapper tAbbGoodsMapper;

	@GetMapping("view")
	@RequiresPermissions("gen:tAbbCGoods:view")
    public String view(Model model)
    {
		String str="";
		setTitle(model, new TitleVo("列表", str+"管理", true,"欢迎进入"+str+"页面", true, false));
        return prefix + "/list";
    }


	@PostMapping("list")
	@RequiresPermissions("gen:tAbbCGoods:list")
	@ResponseBody
	public Object list(Tablepar tablepar,String searchTxt){
		PageInfo<TAbbGoods> page=tAbbGoodsService.list(tablepar,searchTxt,"",2) ;
		TableSplitResult<TAbbGoods> result=new TableSplitResult<TAbbGoods>(page.getPageNum(), page.getTotal(), page.getList());
		return  result;
	}

	/**
     * 新增
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        return prefix + "/add";
    }


	@PostMapping("add")
	@RequiresPermissions("gen:tAbbCGoods:add")
	@ResponseBody
	public AjaxResult add(TAbbGoods tAbbGoods,Model model, String[] goodsimagesarray){
		String goodsimages="";
		for (String image:goodsimagesarray){
			if (goodsimages.equals("")){
				goodsimages=goodsimages+image;
			}else{
				goodsimages=goodsimages+","+image;
			}
		}
		tAbbGoods.setGoodsimages(goodsimages);
		tAbbGoods.setType(2);
        tAbbGoods.setCreattime(DateUtil.getCurrentDateTimeStr());
        tAbbGoods.setUpdatetime(DateUtil.getCurrentDateTimeStr());
        tAbbGoods.setGoodsbianhao(SnowflakeIdWorker.getUUID());
		int b=tAbbGoodsService.insertSelective(tAbbGoods);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}

	/**
	 * 删除用户
	 * @param ids
	 * @return
	 */
	@PostMapping("remove")
	@RequiresPermissions("gen:tAbbCGoods:remove")
	@ResponseBody
	public AjaxResult remove(String ids){
		int b=tAbbGoodsService.deleteByPrimaryKey(ids);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}



	/**
	 * 修改跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("TAbbGoods", tAbbGoodsService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }

	/**
     * 修改保存
     */
    @RequiresPermissions("gen:tAbbCGoods:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TAbbGoods record)
    {
        return toAjax(tAbbGoodsService.updateByPrimaryKeySelective(record));
    }
	/**
	 * 置顶
	 */
	@RequiresPermissions("gen:tAbbCGoods:zding")
	@PostMapping("/zding")
	@ResponseBody
	public AjaxResult zding(TAbbGoods record)
	{
		record.setUpdatetime(DateUtil.getCurrentDateTimeStr());
		return toAjax(tAbbGoodsService.updateByPrimaryKeySelective(record));
	}
	/**
	 * 设为热推
	 */
	@RequiresPermissions("gen:tAbbCGoods:retui")
	@PostMapping("/retui")
	@ResponseBody
	public AjaxResult retui(TAbbGoods record)
	{
		TAbbGoodsExample testExample=new TAbbGoodsExample();
		testExample.createCriteria().andIshotpushEqualTo(1);
		List<TAbbGoods> list= tAbbGoodsMapper.selectByExample(testExample);
		if (list!=null && list.size()>=3){
			return toAjax(0);
		}else{
			record.setIshotpush(1);
			return toAjax(tAbbGoodsService.updateByPrimaryKeySelective(record));
		}
	}
	/**
	 * 取消热推
	 */
	@RequiresPermissions("gen:tAbbCGoods:quxiao")
	@PostMapping("/quxiao")
	@ResponseBody
	public AjaxResult quxiao(TAbbGoods record)
	{
		record.setIshotpush(0);
		return toAjax(tAbbGoodsService.updateByPrimaryKeySelective(record));
	}
	/**
	 * 推荐
	 */
	@RequiresPermissions("gen:tAbbCGoods:tuijian")
	@PostMapping("/tuijian")
	@ResponseBody
	public AjaxResult tuijian(TAbbGoods record)
	{
		    tAbbGoodsService.updateYuliu3ByType(record.getType());
			return toAjax(tAbbGoodsService.updateByPrimaryKeySelective(record));
	}
	/**
	 * 取消推荐
	 */
	@RequiresPermissions("gen:tAbbCGoods:qtuijian")
	@PostMapping("/qtuijian")
	@ResponseBody
	public AjaxResult qtuijian(TAbbGoods record)
	{
			return toAjax(tAbbGoodsService.updateByPrimaryKeySelective(record));
	}
	/**
	 * 上架
	 */
	@RequiresPermissions("gen:tAbbCGoods:shangjia")
	@PostMapping("/shangjia")
	@ResponseBody
	public AjaxResult shangjia(TAbbGoods record)
	{
		record.setIsdel(0);
		return toAjax(tAbbGoodsService.updateByPrimaryKeySelective(record));
	}
	/**
	 * 下架
	 */
	@RequiresPermissions("gen:tAbbCGoods:xiajia")
	@PostMapping("/xiajia")
	@ResponseBody
	public AjaxResult xiajia(TAbbGoods record)
	{
		record.setIsdel(1);
		return toAjax(tAbbGoodsService.updateByPrimaryKeySelective(record));
	}




}
